using MediatR;
using Microsoft.AspNetCore.Mvc;
using Warehouse.API.Read.App.Commands;
using Warehouse.Dto;

namespace Warehouse.API.Read.Controllers
{
    /// <summary>
    /// 调拨单查询控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AllocationQueryController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly Warehouse.Services.IServices.IAllocationService _allocationService;

        public AllocationQueryController(IMediator mediator, Warehouse.Services.IServices.IAllocationService allocationService)
        {
            _mediator = mediator;
            _allocationService = allocationService;
        }

        /// <summary>
        /// 获取调拨单列表（分页查询）
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="transferNo">调拨单号</param>
        /// <param name="fromWarehouseId">调出仓库ID</param>
        /// <param name="toWarehouseId">调入仓库ID</param>
        /// <param name="billStatus">单据状态</param>
        /// <param name="outStatus">出库状态</param>
        /// <param name="inStatus">入库状态</param>
        /// <param name="beginDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>分页查询结果</returns>
        [HttpGet("GetTransferList")]
        public async Task<IActionResult> GetTransferList(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10,
            [FromQuery] string? transferNo = null,
            [FromQuery] int? fromWarehouseId = null,
            [FromQuery] int? toWarehouseId = null,
            [FromQuery] byte? billStatus = null,
            [FromQuery] byte? outStatus = null,
            [FromQuery] byte? inStatus = null,
            [FromQuery] DateTime? beginDate = null,
            [FromQuery] DateTime? endDate = null)
        {
            try
            {
                var query = new AllocationListQuery
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TransferNo = transferNo,
                    FromWarehouseId = fromWarehouseId,
                    ToWarehouseId = toWarehouseId,
                    BillStatus = billStatus,
                    OutStatus = outStatus,
                    InStatus = inStatus,
                    BeginDate = beginDate,
                    EndDate = endDate
                };

                var result = await _mediator.Send(query);

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨操作日志
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        [HttpGet("GetTransferOpLogs/{transferId}")]
        public async Task<IActionResult> GetTransferOpLogs(int transferId)
        {
            try
            {
                var result = await _allocationService.GetTransferOpLogsAsync(transferId);
                return Ok(new { code = 200, message = "查询成功", data = result });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { code = 500, message = "服务器内部错误", error = ex.Message });
            }
        }

        /// <summary>
        /// 获取调拨单详情
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        /// <returns>调拨单详情</returns>
        [HttpGet("GetTransferDetail")]
        public async Task<IActionResult> GetTransferDetail([FromQuery] int transferId)
        {
            try
            {
                var query = new AllocationDetailQuery
                {
                    TransferId = transferId
                };

                var result = await _mediator.Send(query);

                if (result == null)
                {
                    return NotFound(new
                    {
                        code = 404,
                        message = "调拨单不存在"
                    });
                }

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨出库记录
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        /// <returns>出库记录列表</returns>
        [HttpGet("GetTransferOutRecords/{transferId}")]
        public async Task<IActionResult> GetTransferOutRecords(int transferId)
        {
            try
            {
                var query = new TransferOutRecordsQuery
                {
                    TransferId = transferId
                };

                var result = await _mediator.Send(query);

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨入库记录
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        /// <returns>入库记录列表</returns>
        [HttpGet("GetTransferInRecords/{transferId}")]
        public async Task<IActionResult> GetTransferInRecords(int transferId)
        {
            try
            {
                var query = new TransferInRecordsQuery
                {
                    TransferId = transferId
                };

                var result = await _mediator.Send(query);

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }
    }
}
